﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;
public class RankMenuLogic : SQMenuBase
{
    public Transform rankMenu;
    private string Path = "Client/UI640/";
    private RankData _rankListData;
    private float rankItemInitPosX = 21f;
    private float rankItemInitPosY = 45f;
    float lineSpace = -90f;

    public override void OnClick(GameObject btn)
    {
        switch (btn.name)
        {
            case "btnRankClose":
                btn.gameObject.GetComponent<UIPlaySound>().Play();
                UIManager.Instance.unpopMenu();
                break;
            default:
                break;
        }
    }
    public override void onStart()
    {
        base.onStart();
        shopData();
        initShopView();

    }

    public void shopData()
    {

        _rankListData = JsonMapper.ToObject<RankData>(rankTestStr);

    }

    // 初始化显示
    public void initShopView()
    {
        Object rankItemBg = Resources.Load(Path + "rankItemBg");
        Transform scroll = rankMenu.transform.Find("imgRankCase/ScrollView").transform;
        int itemNumber = _rankListData.list.Count;
        for (int j = 0; j < itemNumber; j++)
        {
            GameObject rankBG = (GameObject)Instantiate(rankItemBg);
            rankBG.name = "btnRich" + j;
            SparkExtensions.setParent(rankBG, scroll.gameObject, rankItemInitPosX, (rankItemInitPosY + lineSpace * j));
            foreach (System.Reflection.PropertyInfo p in _rankListData.list[j].GetType().GetProperties())
            {
                string childName = "txt_" + p.Name;
                string showTxt = p.GetValue(_rankListData.list[j], null).ToString();
                SparkExtensions.SetChildText(rankBG, childName, showTxt);

            }
            if (j < 3)//显示前3名，else是其他名次
            {
                GameObject rank = rankBG.FindChild("rank");
                SetImgForObj(rank.transform, Path + "RankMenu/imgRank" + (j + 1));
            }
            else
            {
                SparkExtensions.SetChildText(rankBG, "txtRank", _rankListData.list[j].ranking.ToString());
            }

        }
        SparkExtensions.SetChildText(rankMenu.gameObject, "imgPlayCase/txt_PlayName", GlobalDataManager.instance.playerData.userName);
        SparkExtensions.SetChildText(rankMenu.gameObject, "imgPlayCase/txt_price", GlobalDataManager.instance.playerData.price.ToString());
        SparkExtensions.SetChildText(rankMenu.gameObject, "imgPlayCase/txt_score", GlobalDataManager.instance.playerData.score.ToString());
        SparkExtensions.SetChildText(rankMenu.gameObject, "imgPlayCase/txt_rank", GlobalDataManager.instance.playerData.rank.ToString());
    }
    private string rankTestStr = @"
 {
    ""list"": [
        {
            ""ranking"": 1,
            ""userName"": ""caojianhua"",
            ""price"": 1000,
            ""level"": 10,
            ""title"": ""Q圣"",
            ""icon"": ""url""
        },
        {
            ""ranking"": 2,
            ""userName"": ""caojianhua1"",
            ""price"": 2000,
            ""level"": 20,
            ""title"": ""Q神"",
            ""icon"": ""url""
        },
        {
            ""ranking"": 3,
            ""userName"": ""caojianhua2"",
            ""price"": 3000,
            ""level"": 30,
            ""title"": ""Q圣"",
            ""icon"": ""url""
        },
        {
            ""ranking"": 4,
            ""userName"": ""caojianhua"",
            ""price"": 1000,
            ""level"": 10,
            ""title"": ""Q圣"",
            ""icon"": ""url""
        },
        {
            ""ranking"": 5,
            ""userName"": ""caojianhua1"",
            ""price"": 2000,
            ""level"": 20,
            ""title"": ""Q神"",
            ""icon"": ""url""
        },
        {
            ""ranking"": 6,
            ""userName"": ""caojianhua2"",
            ""price"": 3000,
            ""level"": 30,
            ""title"": ""Q圣"",
            ""icon"": ""url""
        },
        {
            ""ranking"": 6,
            ""userName"": ""caojianhua2"",
            ""price"": 3000,
            ""level"": 30,
            ""title"": ""Q圣"",
            ""icon"": ""url""
        },
        {
            ""ranking"": 6,
            ""userName"": ""caojianhua2"",
            ""price"": 3000,
            ""level"": 30,
            ""title"": ""Q圣"",
            ""icon"": ""url""
        },
        {
            ""ranking"": 6,
            ""userName"": ""caojianhua2"",
            ""price"": 3000,
            ""level"": 30,
            ""title"": ""Q圣"",
            ""icon"": ""url""
        }
    ]
}";
}
